<?
class CChat
{
	public $TYPE_MESSAGE = array();
	
	function __construct()
	{
		$TYPE_MESSAGE = array(
			'LOGIN',
			'LOGOUT',
			'MESSAGE',
		);
	}
	
	function AddMessage($arFields)
	{
		global $DB;
		
		$insert["CREATED_BY"]         = MySQL::SQLValue($arFields['CREATED_BY'], MySQL::SQLVALUE_NUMBER);
		$insert["USER_NAME"]          = MySQL::SQLValue($arFields['USER_NAME'], MySQL::SQLVALUE_TEXT);
		$insert["MESSAGE"]            = MySQL::SQLValue($arFields['MESSAGE'], MySQL::SQLVALUE_TEXT);
		$insert["TYPE_MESSAGE"]       = MySQL::SQLValue($arFields['TYPE_MESSAGE'], MySQL::SQLVALUE_TEXT);
		$insert["SEARCHABLE_CONTENT"] = MySQL::SQLValue(CTool::ToUpper($arFields['MESSAGE']), MySQL::SQLVALUE_TEXT);
		
		$result = $DB->InsertRow("b_chat_message", $insert);
		
		if($result)
		{
		   return $result;
		}
		else
		{
		    //echo $DB->Error();
		    $DB->Kill();
		}
		return false;
	}
	
	function GetListMessages($arFields)
	{
		global $APPLICATION, $DB;
		
		$WHERE = '';
		
		if($arFields['ID'])
		{
			$WHERE = "WHERE ID > ".$arFields['ID'];
		}
		
		$sql = "SELECT * FROM b_chat_message ".$WHERE;
	
		if(!$DB->Query($sql))
		{
			//echo $DB->Error();
			$DB->Kill();
		}
		
		while($arMessage = $DB->Fetch())
		{
		    $a[] = $arMessage;
		}

		return $a;
	}
	
	function GetList($arSort = NULL, $arFilter = array(), $arNavigation = array())
	{
		global $APPLICATION, $DB;
		
		$WHERE = $DB->CreatWhereFilter($arFilter);
		$LIMIT = $DB->CreatLimitFilter($arNavigation);
		
		$sql = "SELECT * FROM b_chat_message".$WHERE.$LIMIT;
		$cnt_sql = "SELECT COUNT(*) FROM b_chat_message ".$WHERE;

		
		if(!$DB->Query($sql))
		{
			$APPLICATION->SetError($DB->Error());
			//$DB->Kill();
			return false;
		}	
		
		return $DB;
		
//		if(!$cnt = $DB->RowCount())
//		{
//			$DB->Kill();
//		}
//		var_dump($cnt);
//		
//		while($arMessage = $DB->Fetch())
//		{
//		    $a[] = $arMessage;
//		}
//
//		return $a;
	}
}